# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

EAPI=5
inherit multilib-minimal eutils autotools

MY_PN="libeatmydata"

DESCRIPTION="LD_PRELOAD hack to stop reads from thrashing other program's caches"
HOMEPAGE="https://launchpad.net/libeatmydata/"
SRC_URI="https://launchpad.net/${MY_PN}/trunk/release-${PV}/+download/${MY_PN}-${PV}.tar.gz"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="x86 amd64"
IUSE=""

# sandbox fools LD_PRELOAD and libeatmydata does not get control
# bug/feature in sandbox?
#DEPEND="test? ( dev-util/strace )"
RESTRICT=test

DEPEND="sys-apps/sed"
RDEPEND=""

S="${WORKDIR}/${MY_PN}-${PV}"

cachesave_recurse()
{
	target="$1"
#	printf "Processing file/directory $target\n"
	newtarget="${target//eatmydata/cachesave}"

	if [[ $target = *eatmydata* ]]; then
		# replace all matches
#		echo mv "$target" "$newtarget"
		mv "$target" "$newtarget"
	fi

	target="$newtarget"

	if [[ -d $target ]]; then
		for entry in "$target"/*; do
			cachesave_recurse "$entry"
		done
	fi

}

src_prepare() {
	epatch "${FILESDIR}/${PN}-105-undpkg.patch"

	for f in $(grep -R eatmydata -l); do
		sed -i -e "s/eatmydata/cachesave/g" "$f"
	done
	cachesave_recurse .

	# overwrite main file
	cp "${FILESDIR}/${PN}-105.c" "${S}/libcachesave/libcachesave.c"

	eautoreconf
	multilib_copy_sources
}

multilib_src_install_all() {
	prune_libtool_files --all

	dodoc AUTHORS README
}
